package com.raycloud.robot.utils;

import org.slf4j.LoggerFactory;
import java.io.IOException;
import org.apache.commons.httpclient.HttpMethod;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.HttpClient;
import com.raycloud.bizlogger.log.support.NetUtils;
import com.raycloud.bizlogger.log.support.LoggerHandler;
import com.raycloud.robot.enums.EnumThreadPoolType;
import org.slf4j.Logger;

public class DingDingUtils{
    private static Logger log;
    private static final String THREAD_POOL_ALERT_URL="https://oapi.dingtalk.com/robot/send?access_token=99a394fbd48cd1790468ea5fb51b4a5234ce7ebe03609aab505812286f49c09a";
    public static void sendThreadPoolMsg(final Integer maximumPoolSize,final Integer activeCount,final String port,final EnumThreadPoolType threadPoolType,final String threadPoolName){
        try{
            final StringBuilder builder=new StringBuilder();
            builder.append("### ");
            builder.append(LoggerHandler.NAME);
            builder.append("\u7ebf\u7a0b\u6c60\u76f8\u5173\u76d1\u63a7\u544a\u8b66\r\n");
            final String host=NetUtils.getLocalHost();
            if(host.contains("192.168.82")){
                builder.append("####").append(" \u6d4b\u8bd5\u73af\u5883").append("(").append(host).append(")").append("\r\n");
            }
            else if(host.contains("192.168.84")){
                builder.append("####").append(" \u751f\u4ea7\u73af\u5883").append("(").append(host).append(")").append("\r\n");
            }
            else{
                builder.append("####").append(" \u672a\u77e5\u73af\u5883").append("(").append(host).append(")").append("\r\n");
            }
            builder.append("####").append(" ").append("\u7ebf\u7a0b\u6c60\u7c7b\u578b\u4e3a\uff1a ").append(threadPoolType.getName()).append("\r\n");
            switch(threadPoolType){
                case DUBBO_SERVER_THREAD_POOL:{
                    builder.append("####").append(" ").append("\u7ebf\u7a0b\u6c60\u540d\u79f0\u4e3a\uff1a ").append(threadPoolName).append("\r\n");
                    builder.append("####").append(" ").append("\u670d\u52a1\u63d0\u4f9b\u8005\u7aef\u53e3\u4e3a\uff1a ").append(port).append("\r\n");
                    break;
                }
            }
            final String format=String.format("\u6700\u5927\u7ebf\u7a0b\u6570\u4e3a\t\u300c%d\u300d\n  \u6d3b\u8dc3\u7ebf\u7a0b\u6570\u4e3a\t\u300c%d\u300d",maximumPoolSize,activeCount);
            builder.append("####").append(" ").append(format).append("\r\n");
            commonSendMsg("https://oapi.dingtalk.com/robot/send?access_token=99a394fbd48cd1790468ea5fb51b4a5234ce7ebe03609aab505812286f49c09a",LoggerHandler.NAME+"\u7684dubbo\u7ebf\u7a0b\u6c60\u76f8\u5173\u76d1\u63a7\u544a\u8b66",builder.toString());
        }
        catch(Throwable e){
            DingDingUtils.log.error("[\u8c03\u7528\u9489\u9489\u673a\u5668\u4eba\u51fa\u73b0\u5176\u4ed6\u9519\u8bef][{}][{}]",e.getMessage(),e);
        }
    }
    public static void commonSendMsg(final String url,final String title,final String message) throws IOException{
        final HttpClient client=new HttpClient();
        client.setConnectionTimeout(6000);
        client.setTimeout(6000);
        final PostMethod method=new PostMethod(url);
        method.addRequestHeader("Content-Type","application/json;charset=UTF-8");
        final JSONObject object=new JSONObject();
        object.put("msgtype","markdown");
        final JSONObject linkObject=new JSONObject();
        linkObject.put("title",title);
        linkObject.put("text",message);
        object.put("markdown",linkObject);
        method.setRequestBody(object.toJSONString());
        final int i=client.executeMethod((HttpMethod)method);
    }
    public static void main(final String[] args){
        sendThreadPoolMsg(200,100,"20881",EnumThreadPoolType.DUBBO_SERVER_THREAD_POOL,"DubboServerHandler-172.16.75.128");
    }
    static{
        DingDingUtils.log=LoggerFactory.getLogger(DingDingUtils.class);
    }
}
